/**
 *
 */
package de.cbf.cam.dsa.rcp.model.test.mock;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.cbf.cam.dsa.item.model.entities.constants.QualityConstants;
import de.cbf.cam.dsa.item.model.entities.constants.ResolveAttributesConstants;
import de.cbf.cam.dsa.item.model.test.mock.MockItemResolver;
import de.cbf.cam.dsa.rcp.model.entities.PerkDSA;
import de.cbf.cam.dsa.rcp.model.entities.ProfessionDSA;
import de.cbf.cam.dsa.rcp.model.entities.ProfessionLevel;
import de.cbf.cam.dsa.rcp.model.entities.items.ItemForProfession;
import de.cbf.cam.dsa.rcp.model.entities.items.ItemForProfessionAlternatives;
import de.cbf.cam.dsa.rcp.model.entities.items.ItemForProfessionTypical;
import de.cbf.cam.dsa.rcp.model.entities.items.ProfessionItemDSA;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.AbstractParameteredPersister;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.IRCPPersisterProfession;
import de.cbf.util.uuidheader.entities.NamedEntity;
import de.cbf.util.uuidheader.entities.UUIDReference;

/**
 * @author salter
 *
 */
public class MockRCPProfessionSupplier extends AbstractParameteredPersister implements IRCPPersisterProfession {
	private static final Log logger = LogFactory.getLog(MockRCPProfessionSupplier.class);

	private static final String NAME = "Mock profession supplier";

	public static final String PROFESSION_LEVEL_CHILD 		= "Kind";
	public static final String PROFESSION_LEVEL_APPRENTICE	= "Lehrling";
	public static final String PROFESSION_LEVEL_JOURNEYMAN	= "Geselle";
	public static final String PROFESSION_LEVEL_MASTER		= "Meister";

	public static final String PROFESSION_FUHRMANN 		= "Fuhrmann";
	public static final String PROFESSION_TAUGENICHTS	= "Taugenichts";
//	public static final String ALCHEMIST	= "Alchemist";

	protected static MockRCPProfessionSupplier instance;

	protected List<ProfessionDSA> professionsList = new ArrayList<ProfessionDSA>();

	protected MockItemResolver mockItems = MockItemResolver.getInstance();
	/**
	 *
	 */
	public MockRCPProfessionSupplier() {
		setName(NAME);

		initProfessions();
	}

	public static MockRCPProfessionSupplier getInstance() {
		if(instance == null){
			instance = new MockRCPProfessionSupplier();
		}
		return instance;
	}

	@Override
	public Collection<Object> getKnownLoadParameters() {
		List<Object> result = new ArrayList<Object>();
		return result;
	}
	@Override
	public Collection<Object> getKnownSaveParameters() {
		List<Object> result = new ArrayList<Object>();
		return result;
	}

	public ProfessionDSA getProfessionByName(String name){
		ProfessionDSA result = null;

		for (Iterator<ProfessionDSA> iterator = professionsList.iterator(); iterator.hasNext();) {
			ProfessionDSA culture = (ProfessionDSA) iterator.next();
			if(culture.getName().equals(name)){
				result = culture;
			}
		}
		if(result == null){
			logger.error("could not find profession by name: "+name);
		}
		return result;
	}

	@Override
	public ProfessionDSA loadProfession(UUID uuid) {
		for (ProfessionDSA profession : professionsList) {
			if(profession.getUuid().toString().equals(uuid.toString())){
				return profession;
			}
		}
		return null;
	}

	@Override
	public Collection<ProfessionDSA> loadAllProfessions() {
		return new ArrayList<ProfessionDSA>(professionsList);
	}

	@Override
	public void saveProfession(ProfessionDSA entity) {
		logger.warn("Saving profession to mock: "+entity);
		professionsList.add(entity);
	}

	/**
	 *
	 */
	protected void initProfessions() {
		professionsList.clear();

		ProfessionDSA testProfession;

		testProfession = buildFuhrmann();
		professionsList.add(testProfession);

		testProfession = buildTaugenichts();
		professionsList.add(testProfession);


	}


	protected ProfessionDSA buildFuhrmann(){
		//generate profession levels
		ProfessionItemDSA testProfession;
		Map<String, ProfessionLevel> professionLevels = new HashMap<String, ProfessionLevel>();
		ProfessionLevel level;
		level = new ProfessionLevel(PROFESSION_LEVEL_CHILD,3,8);
		level.setUuid(UUID.fromString("47649420-6638-41fe-a0e3-e800d49d40e5"));
		professionLevels.put(level.getName(), level);

		level = new ProfessionLevel(PROFESSION_LEVEL_JOURNEYMAN,9,25);
		level.setUuid(UUID.fromString("72197f1e-3302-4c4f-866f-62b6473f0a38"));
		professionLevels.put(level.getName(), level);

		level = new ProfessionLevel(PROFESSION_LEVEL_MASTER,23,70);
		level.setUuid(UUID.fromString("ba55f1a5-1167-47ec-8933-ca5afe165d19"));
		professionLevels.put(level.getName(), level);

		testProfession = new ProfessionItemDSA();
		testProfession.setName(PROFESSION_FUHRMANN);
		testProfession.setUuid(UUID.fromString("2aab20c9-db16-423e-a95d-c0ef854e636d"));
		testProfession.setProfessionLevels(professionLevels );
		testProfession.setWeightedPerks(createPerksFuhrmann());
		testProfession.setProfessionItems(createItemsFuhrmann());

		return testProfession;
	}



	protected List<PerkDSA> createPerksFuhrmann(){
		//add perks
		List<PerkDSA> perks = new ArrayList<PerkDSA>();
		PerkDSA perk;
		NamedEntity entity;

		entity = new NamedEntity();
		entity.setName("lispelt");
		entity.setUuid(UUID.fromString("675f5f7d-460e-4a7d-9fc7-2747f8c12ee9"));
		perk = new PerkDSA(entity, 0D, true);
		perk.setUuid(UUID.fromString("dc2ccc5a-1148-45aa-8a05-5d720b660df8"));
		perks.add(perk);

		entity = new NamedEntity();
		entity.setName("verschwitzt");
		entity.setUuid(UUID.fromString("aa4a8b5d-c0e9-4c4c-a7c8-10b84f995cf4"));
		perk = new PerkDSA(entity, 10D, false);
		perk.setUuid(UUID.fromString("ed5f324a-1dec-45dc-a176-0f0cda1ae557"));
		perks.add(perk);

		return perks;
	}

	protected List<ItemForProfession> createItemsFuhrmann(){
		//add perks
		List<ItemForProfession> items = new ArrayList<ItemForProfession>();
		ItemForProfession item;
		UUIDReference reference;
		ItemForProfessionTypical itemTypical;

		ItemForProfessionAlternatives itemAlternatives = new ItemForProfessionAlternatives("Waffen:Fuhrmann");
		itemAlternatives.setUuid(UUID.fromString("2aad8f13-ffc3-4f94-a8f0-4bf1cdf3dfd8"));

		reference = UUIDReference.extractUUIDReference(mockItems.getTestItemByName(MockItemResolver.FUHRMANNSPEITSCHE));
		item = new ItemForProfession();
		item.setUuid(UUID.fromString("5daff3fe-212b-44fa-9076-6b37d490fa1c"));
		item.setItemReference(reference);
		itemAlternatives.addAlternativeItem(reference, 10D);

		reference = UUIDReference.extractUUIDReference(mockItems.getTestItemByName(MockItemResolver.HAUSWEHR));
		item = new ItemForProfession();
		item.setUuid(UUID.fromString("eb1084c8-64a9-4cdd-a75a-553eea7dbe43"));
		item.setItemReference(reference);
		itemAlternatives.addAlternativeItem(reference, 5D);
		items.add(itemAlternatives);

		reference = UUIDReference.extractUUIDReference(mockItems.getTestItemByName(MockItemResolver.WETTERFEST_MANTEL));
		item = new ItemForProfession();
		item.setUuid(UUID.fromString("1e94eddc-02c5-4fd0-b392-42d231f143e8"));
		item.setItemReference(reference);
		items.add(item);

		NamedEntity cultTypical = new NamedEntity("kulturtypische Kleidung(verbreitet)");
		cultTypical.setUuid(UUID.fromString("880064af-c90e-4740-9eb5-c906e7a963c0"));
		reference = UUIDReference.extractUUIDReference(cultTypical) ;
		itemTypical = new ItemForProfessionTypical();
		itemTypical.setUuid(UUID.fromString("fc324cc7-f13d-4377-a0ae-87520ab32231"));
		itemTypical.addResolveAttribute(ResolveAttributesConstants.ATTRIBUTE_CLOTHING);
		itemTypical.setDesiredQuality(QualityConstants.QUALITY_COMMON);
		itemTypical.setItemReference(reference);
		items.add(itemTypical);

		return items;
	}

	protected ProfessionDSA buildTaugenichts() {
		ProfessionItemDSA testProfession;
		Map<String, ProfessionLevel> professionLevels = new HashMap<String, ProfessionLevel>();
		ProfessionLevel level;
		level = new ProfessionLevel(PROFESSION_LEVEL_CHILD,3,8);
		level.setUuid(UUID.fromString("379055bc-73c0-4953-801a-b1362892a97e"));
		professionLevels.put(level.getName(), level);

		level = new ProfessionLevel(PROFESSION_LEVEL_APPRENTICE,9,21);
		level.setUuid(UUID.fromString("cedb1110-e865-4662-9b86-1b0195b4b4aa"));
		professionLevels.put(level.getName(), level);

		level = new ProfessionLevel(PROFESSION_LEVEL_JOURNEYMAN,17,30);
		level.setUuid(UUID.fromString("f21f1359-33c5-48af-8bc3-fbb80093068c"));
		professionLevels.put(level.getName(), level);

		level = new ProfessionLevel(PROFESSION_LEVEL_MASTER,27,70);
		level.setUuid(UUID.fromString("a2472966-cb5f-453c-a6a0-47194d697b7c"));
		professionLevels.put(level.getName(), level);

		testProfession = new ProfessionItemDSA();
		testProfession.setName(PROFESSION_TAUGENICHTS);
		testProfession.setUuid(UUID.fromString("1bb90b3e-732d-44ea-8591-94a948b4929d"));
		testProfession.setProfessionLevels(professionLevels );
		testProfession.setWeightedPerks(createPerksTaugenichts());
		testProfession.setProfessionItems(createItemsTaugenichts());

		return testProfession;
	}


	protected List<PerkDSA> createPerksTaugenichts(){
		//add perks
		List<PerkDSA> perks = new ArrayList<PerkDSA>();
		PerkDSA perk;
		NamedEntity entity;

		entity = new NamedEntity();
		entity.setName("gut aussehend");
		entity.setUuid(UUID.fromString("1ff2723d-2d16-4f17-9dfe-d09d3cb784d1"));
		perk = new PerkDSA(entity, 2D, false);
		perk.setUuid(UUID.fromString("6be564eb-9c69-45f8-a531-1d19b85fa2d3"));
		perks.add(perk);

		entity = new NamedEntity();
		entity.setName("adelige Abstammung");
		entity.setUuid(UUID.fromString("bc745097-8eaa-4ebb-a559-4a3b2508953d"));
		perk = new PerkDSA(entity, 0D, true);
		perk.setUuid(UUID.fromString("05e3e001-f65b-4b73-9021-e29eaab8524d"));
		perk.addAttribute(PerkDSA.ATTRIBUTE_ROYAL);
		perks.add(perk);

		return perks;
	}

	protected List<ItemForProfession> createItemsTaugenichts(){
		//add perks
		List<ItemForProfession> items = new ArrayList<ItemForProfession>();
		ItemForProfession item;
		UUIDReference reference;
		ItemForProfessionTypical itemTypical;

		ItemForProfessionAlternatives itemAlternatives = new ItemForProfessionAlternatives("Waffen:Taugenichts");
		itemAlternatives.setUuid(UUID.fromString("2aad8f13-ffc3-4f94-a8f0-4bf1cdf3dfd8"));

		reference = UUIDReference.extractUUIDReference(mockItems.getTestItemByName(MockItemResolver.DEGEN));
		item = new ItemForProfession();
		item.setUuid(UUID.fromString("5a6c0140-2ebe-49f4-ac97-7246943d06ae"));
		item.setItemReference(reference);
		itemAlternatives.addAlternativeItem(reference, 10D);

		reference = UUIDReference.extractUUIDReference(mockItems.getTestItemByName(MockItemResolver.FLORETT));
		item = new ItemForProfession();
		item.setUuid(UUID.fromString("7cd2bc07-7828-4dd8-b0a7-52e4b4dcf8c1"));
		item.setItemReference(reference);
		itemAlternatives.addAlternativeItem(reference, 10D);

		reference = UUIDReference.extractUUIDReference(mockItems.getTestItemByName(MockItemResolver.HAUSWEHR));
		item = new ItemForProfession();
		item.setUuid(UUID.fromString("1b08b67c-3d7e-4c1d-8f35-c699b2e62f55"));
		item.setItemReference(reference);
		itemAlternatives.addAlternativeItem(reference, 5D);

		items.add(itemAlternatives);

		reference = UUIDReference.extractUUIDReference(mockItems.getTestItemByName(MockItemResolver.RUESCHEN_HEMD));
		item = new ItemForProfession();
		item.setUuid(UUID.fromString("ecd03abb-b698-4777-b55a-a7c84725fd32"));
		item.setItemReference(reference);
		items.add(item);

		NamedEntity cultTypical = new NamedEntity("kulturtypische Kleidung(besser)");
		cultTypical.setUuid(UUID.fromString("38c55edb-e827-42ad-a595-5ab24be01e0c"));
		reference = UUIDReference.extractUUIDReference(cultTypical) ;
		itemTypical = new ItemForProfessionTypical();
		itemTypical.setUuid(UUID.fromString("abf17095-78a3-46fc-bbf2-3bf0d270fe7e"));
		itemTypical.addResolveAttribute(ResolveAttributesConstants.ATTRIBUTE_CLOTHING);
		itemTypical.setDesiredQuality(QualityConstants.QUALITY_GOOD);
		itemTypical.setItemReference(reference);
		items.add(itemTypical);

		return items;
	}


	@Override
	public void tidyUpProfessions(List<UUID> uuids) {
		// nothing to do
	}
}
